
$(function(){
// standard global variables
var container, scene, camera, renderer, controls, stats;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
// custom global variables
var cube;

    init();
    animate();


// FUNCTIONS
function init()
{
    // SCENE
    scene = new THREE.Scene();
    // CAMERA
    var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
    var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
    camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
    scene.add(camera);
    camera.position.set(0,50,400);
    camera.lookAt(scene.position);
    // RENDERER
    if ( Detector.webgl )
        renderer = new THREE.WebGLRenderer( {antialias:true} );
    else
        renderer = new THREE.CanvasRenderer();
    renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
    container = document.getElementById( '3dBackGround' );
    container.appendChild( renderer.domElement );
    // EVENTS
    THREEx.WindowResize(renderer, camera);
    THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) });
    // CONTROLS
    controls = new THREE.OrbitControls( camera );
    controls.autoRotate=true;
    controls.autoRotateSpeed=0.5;
    // STATS
    //stats = new Stats();
    //stats.domElement.style.position = 'absolute';
    //stats.domElement.style.bottom = '0px';
    //stats.domElement.style.zIndex = 100;
    //container.appendChild( stats.domElement );
    // LIGHT
    var light = new THREE.PointLight(0xffffff);
    light.position.set(0,250,0);
    scene.add(light);
    // FLOOR
    //var floorTexture = new THREE.ImageUtils.loadTexture( 'stemkoski/Three.js/images/checkerboard.jpg' );
    //floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping;
    //floorTexture.repeat.set( 10, 10 );
    //var floorMaterial = new THREE.MeshBasicMaterial( { map: floorTexture, side: THREE.DoubleSide } );
    //var floorGeometry = new THREE.PlaneGeometry(100, 100, 10, 10);
    //var floor = new THREE.Mesh(floorGeometry, floorMaterial);
    //floor.position.y = -0.5;
    //floor.rotation.x = Math.PI / 2;
    //scene.add(floor);

    ////////////
    // CUSTOM //
    ////////////

    // axes
    //var axes = new THREE.AxisHelper(100);
    //scene.add( axes );

    //var imagePrefix = "images/dawnmountain-";
    //var directions  = ["xpos", "xneg", "ypos", "yneg", "zpos", "zneg"];
    //var imageSuffix = ".png";
    //var skyGeometry = new THREE.CubeGeometry( 5000, 5000, 5000 );
    //
    //var materialArray = [];
    //for (var i = 0; i < 6; i++)
    //    materialArray.push( new THREE.MeshBasicMaterial({
    //        map: THREE.ImageUtils.loadTexture( imagePrefix + directions[i] + imageSuffix ),
    //        side: THREE.BackSide
    //    }));
    //var skyMaterial = new THREE.MeshFaceMaterial( materialArray );
    //var skyBox = new THREE.Mesh( skyGeometry, skyMaterial );
    //scene.add( skyBox );
    var directions  =  ["images/px.jpg", "images/nx.jpg", "images/py.jpg", "images/ny.jpg", "images/pz.jpg", "images/nz.jpg"];;
    var skyGeometry = new THREE.CubeGeometry( 5000, 5000, 5000 );

    var materialArray = [];
    for (var i = 0; i < 6; i++)
        materialArray.push( new THREE.MeshBasicMaterial({
            map: THREE.ImageUtils.loadTexture(  directions[i]),
            side: THREE.BackSide
        }));
    var skyMaterial = new THREE.MeshFaceMaterial( materialArray );
    var skyBox = new THREE.Mesh( skyGeometry, skyMaterial );
    scene.add( skyBox );
//    var ballGeometry = new THREE.SphereGeometry( 600, 32, 16 );
//
//    var ballTexture = new THREE.ImageUtils.loadTexture('images/uvgrid04.jpg');
//
//    // use "this." to create global object
//    this.customUniforms =
//    {
//        baseTexture: { type: "t", value: ballTexture },
//        mixAmount: 	 { type: "f", value: 0.0 }
//    };
//
//    // create custom material from the shader code above
//    //   that is within specially labeled script tags
//    var customMaterial = new THREE.ShaderMaterial(
//        {
//            uniforms: customUniforms,
//            vertexShader:   document.getElementById( 'vertexShader'   ).textContent,
//            fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
//            side: THREE.DoubleSide
//        }   );
//
//    var ball = new THREE.Mesh( ballGeometry, customMaterial );
//    ball.position.set(0, 65, 0);
//    ball.rotation.set(0, -Math.PI / 2, 0);
//    scene.add( ball );

}

function animate()
{
    requestAnimationFrame( animate );
    render();
    update();
}

function update()
{
    //camera.position.add(new THREE.Vector3(1, 0, 0 ));
    //camera.lookAt(scene.position);
    controls.update();
    //stats.update();
}

function render()
{
    renderer.render( scene, camera );
};
});